1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include <bits/stdc++.h> #define rep(i, x, y) for (int i = x; i <= y; i++) using namespace std;
typedef long long ll; const int N = 5e4 + 10, M = 1e5 + 10; ll n, m, Q, unit; ll fa[N], sz[N], pos[M], mark[M], pre[M], nxt[M]; ll fax[M], fay[M], szx[M], szy[M], ans[M]; struct edge { int x, y, z, id; }e[M], e2[M]; struct node { int x, y, id; }q1[M], q2[M];
bool cmpe(edge a, edge b) { return a.z > b.z; } bool cmpq2(node a, node b) { return a.y > b.y; }
inline int getfa(int x) { return fa[x] == x ? x : getfa(fa[x]); }
inline void merge(int x, int y) { x = getfa(x), y = getfa(y); if (x == y) return; if (sz[x] > sz[y]) swap(x, y); fa[x] = y, sz[y] += sz[x]; }
inline void read(int &x) { int ff = 1, ch; while (!isdigit(ch = getchar())) ch == '-' ? ff = -1 : 1; x = ch & 15; while (isdigit(ch = getchar())) x = (x << 1) + (x << 3) + (ch & 15); x = x * ff; }
inline void solve() { unit = max(unit, 1ll); for (int o = 1; o <= (Q - 1) / unit + 1; ++o) { int l = (o - 1) * unit + 1, r = min(o * unit, Q), l1 = 0, l2 = 0; rep(i, 1, m) mark[i] = pre[i] = 0; rep(i, 1, r - l + 1) { int op; scanf("%d", &op); if (op == 1) { ++l1; read(q1[l1].x), read(q1[l1].y); if (pre[q1[l1].x]) nxt[pre[q1[l1].x]] = l1; pre[q1[l1].x] = l1; nxt[l1] = 0; if (!mark[q1[l1].x]) mark[q1[l1].x] = i; q1[l1].id = i; } else { ++l2; read(q2[l2].x), read(q2[l2].y); q2[l2].id = i; } ans[i] = 0; } memcpy(e2, e, sizeof(*e) * (m + 1)); sort(e + 1, e + m + 1, cmpe); sort(q2 + 1, q2 + l2 + 1, cmpq2);
rep(i, 1, n) fa[i] = i, sz[i] = 1; int pos = 1; rep(i, 1, l2) { while (pos <= m && e[pos].z >= q2[i].y) { if (!mark[e[pos].id]) { merge(e[pos].x, e[pos].y); } ++pos; } rep(j, 1, l1) { if (((!nxt[j] || q1[nxt[j]].id > q2[i].id) && q1[j].y >= q2[i].y && q1[j].id < q2[i].id) || (mark[q1[j].x] > q2[i].id && e2[q1[j].x].z >= q2[i].y)) { int x = getfa(e2[q1[j].x].x), y = getfa(e2[q1[j].x].y); fax[j] = x, fay[j] = y, szx[j] = sz[x], szy[j] = sz[y]; merge(x, y); } } ans[q2[i].id] = sz[getfa(q2[i].x)]; for (int j = l1; j; --j) { if (fax[j]) { int x = fax[j], y = fay[j]; fa[x] = x, fa[y] = y, sz[x] = szx[j], sz[y] = szy[j]; fax[j] = fay[j] = szx[j] = szy[j] = 0; } } } memcpy(e, e2, sizeof(*e) * (m + 1)); rep(i, 1, l1) e[q1[i].x].z = q1[i].y; rep(i, 1, r - l + 1) if (ans[i]) printf("%lld\n", ans[i]); } }
int main() { cin >> n >> m; unit = sqrt(m * log(m) / log(2)); rep(i, 1, m) { scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z); e[i].id = i; } cin >> Q; solve(); return 0; }
|